local skel = fk.CreateSkill{
  name = "kpaII__youjixinghuo",
}

Fk:loadTranslationTable{
  ["kpaII__youjixinghuo"] = "游击星火",
  [":kpaII__youjixinghuo"] = "你于每个额定回合前有一个无阶段的额外回合。你对一名角色造成伤害后，可将你的一个阶段移动至你于其回合前的额外回合中。",
  ["#kpaII__youjixinghuo_ask"] = "游击星火：你可将你的一个阶段移动至你于 %src 回合前的额外回合中",

  ["@kpaII__youjixinghuo1"] = "游击"
}

skel:addEffect(fk.EventPhaseChanging, {--自己的额定回合失去
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.room.current == player and not player:insideExtraTurn() and player:getMark("kpaII__youjixinghuo2") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    local list = player:getMark("kpaII__youjixinghuo2")
    if table.contains(table.map(list, Util.PhaseStrMapper), data.phase) then
      data.skipped = true
    end
  end,
})

skel:addEffect(fk.BeforeTurnStart, {--额外回合
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and player.room.current == target and not target:insideExtraTurn() and target:getMark("@kpaII__youjixinghuo1") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    local all = {"phase_start","phase_judge","phase_draw","phase_play","phase_discard","phase_finish","Cancel"}
    local phaseList = target:getMark("@kpaII__youjixinghuo1")
    local list = {}
    for _, p in ipairs(all) do
      if table.contains(phaseList,p) then
        table.insert(list, p)
      end
    end
    --因为要排序，不然直接插就行
    player.room:notifySkillInvoked(player, skel.name)
    player:broadcastSkillInvoke(skel.name)
    player:gainAnExtraTurn(false, skel.name, table.map(list, Util.PhaseStrMapper))
  end,
})

skel:addEffect(fk.Damage, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and not data.to.dead
  end,
  on_cost = function (self, event, target, player, data)
    local result = player.room:askToChoice(player,{
      choices = {"phase_start","phase_judge","phase_draw","phase_play","phase_discard","phase_finish","Cancel"},
      prompt = "#kpaII__youjixinghuo_ask:" .. data.to.id,
      skill_name = skel.name,
    })
    if result ~= "Cancel" then
      event:setCostData(self, {result = result})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local phase = event:getCostData(self).result
    for _,p in ipairs(room:getAllPlayers()) do
      room:removeTableMark(p, "@kpaII__youjixinghuo1", phase)
    end
    room:addTableMarkIfNeed(data.to, "@kpaII__youjixinghuo1", phase)
    room:addTableMarkIfNeed(player, "kpaII__youjixinghuo2", phase)
  end,
})

skel:addEffect(fk.Death,{
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name) and target:getMark("@kpaII__youjixinghuo1") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _,phase in ipairs(target:getMark("@kpaII__youjixinghuo1")) do
      room:removeTableMark(target, "@kpaII__youjixinghuo1", phase)
      room:removeTableMark(player, "kpaII__youjixinghuo2", phase)
    end
  end,
})

return skel